﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：StoresManage.cs
  //文件功能描述：库房管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class StoresManage
    {

        private readonly DALStores dalStores = new DALStores(); //库房
        private readonly DALSuppliers dalSupplier = new DALSuppliers();//供货商
        private readonly DALGoodsStocks dalStocks = new DALGoodsStocks(); //库存
        private readonly DALStockIns dalIns = new DALStockIns(); //入库
        private readonly DALStockOuts dalOuts = new DALStockOuts();//出库
        private readonly DALStockLines dalLines = new DALStockLines();
        private readonly DALGoodsStockRcords dalRecords = new DALGoodsStockRcords(); //进出记录；


        #region "保存库房信息 by 赵国伟 2012-04-01"
        /// <summary>
        /// 保存库房信息
        /// </summary>
        /// <param name="store"></param>
        /// <returns></returns>
        public ReturnValue SaveStore(Stores store)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败");
            //检查库房名称是否重复；
            if (dalStores.QueryList(1, "StoreName='" + store.StoreName + "' and StoreID<>" + store.StoreID, "") != null)
            {
                ret.Message = "库房名称重复！";
                return ret;
            }
            int result = 0;
            if (store.StoreID <= 0)
            {
                store.CreateDate = DateTime.Now;
                result = dalStores.Add(store);
            }
            else
            {
                Stores o = dalStores.GetModel(store.StoreID);
                store.CreateDate = o.CreateDate;
                result = dalStores.Update(store);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条库房信息； by 赵国伟 2012-04-01"
        /// <summary>
        /// 查询一条库房信息；
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        [Ajax.AjaxMethod(Ajax.AjaxMethodType.Manager)]
        public Stores GetStoreInfo(int storeId)
        {
            return dalStores.GetModel(storeId);
        }
        #endregion

        #region "删除指定的库房信息； by 赵国伟 2012-04-01"
        /// <summary>
        /// 删除指定的库房信息；
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public ReturnValue DeleteStore(int storeId)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            if (dalStores.Delete(storeId) > 0)
            {
                ret.Message = "删除成功!";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "返回库房信息为Hash结构； by 赵国伟 2012-04-01"
        /// <summary>
        /// 返回库房信息为Hash结构；
        /// </summary>
        public  Dictionary<int,string> QueryStoresToHash()
        {
            return BLLHelper.GenerateToHash(dalStores.QueryStores(false));
        }
        #endregion

        #region "返回库房信息并存为下拉框选项；by 赵国伟 2012-04-01"
        /// <summary>
        /// 返回库房信息并存为下拉框选项；
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryStoresToSelectOption(bool IsAvailable, string defaultValue)
        {
            DALStores dal = new DALStores();
            List<IDNameObj> listOfData = dal.QueryStores(IsAvailable);
            return BLLHelper.GenerateToSelectOption(listOfData, defaultValue);
        }
        #endregion

        #region "查询库房信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 查询库房信息；
        /// </summary>
        /// <returns></returns>
        public List<Stores> QueryStores()
        {
            return dalStores.QueryList("", "StoreID asc");
        }
        #endregion




        #region "保存供应商信息 by 赵国伟 2012-04-04"
        /// <summary>
        /// 保存供应商信息
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ReturnValue SaveSupplier(Suppliers s)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败");
            ///检查名字是否重复；
            if (dalSupplier.QueryList(1, "SupplierName='" + s.SupplierName + "' and SupplierID<>" + s.SupplierID, "") != null)
            {
                ret.Message = "供应商名称重复！";
                return ret;
            }
            int result = 0;
            if (s.SupplierID <= 0)
            {
                s.CreateDate = DateTime.Now;
                result = dalSupplier.Add(s);
            }
            else
            {
                Suppliers o = dalSupplier.GetModel(s.SupplierID);
                s.CreateDate = o.CreateDate;
                result = dalSupplier.Update(s);
            }
            if (result > 0)
            {
                ret.Message = "保存成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询一条供应商信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询一条供应商信息；
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        public Suppliers GetSupplierInfo(int supplierId)
        {
            return dalSupplier.GetModel(supplierId);
        }
        #endregion

        #region "删除指定的供应商信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 删除指定的供应商信息；
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        public ReturnValue DeleteSupplier(int supplierId)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            if (dalSupplier.Delete(supplierId) > 0)
            {
                ret.Message = "删除成功!";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "查询所有的供货商信息； by 赵国伟 2012-04-04"
        /// <summary>
        /// 查询所有的供货商信息
        /// </summary>
        /// <returns></returns>
        public List<Suppliers> QuerySuppliers()
        {
            return dalSupplier.QueryList("", "SupplierID desc ");
        }
        #endregion

        #region "返回供货商信息并存为下拉框选项；by 赵国伟 2012-04-01"
        /// <summary>
        /// 返回供货商信息并存为下拉框选项；
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QuerySuppliersToSelectOption(bool IsAvailable, string defaultValue)
        {
            DALSuppliers dal = new DALSuppliers();
            List<IDNameObj> listOfData = dal.QuerySuppliers(IsAvailable);
            return BLLHelper.GenerateToSelectOption(listOfData, defaultValue);
        }
        #endregion




        #region "库存分页查询； by 赵国伟 2012-06-10"
        /// <summary>
        /// 库存分页查询；
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public List<GoodsStockInfo> QueryStocksAsPage(QueryGoodsStocksAsPage q)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(q.KeyWords))
                cond += " and GoodsName like '" + q.KeyWords + "' ";
            if (q.CategoryID > 0)
                cond += " and CategoryID=" + q.CategoryID;
            if (q.BrandID > 0)
                cond += " and BrandID =" + q.BrandID;
            if (q.StoreID > 0)
                cond += " and StoreID =" + q.StoreID;
            if (!string.IsNullOrEmpty(q.Cond))
                cond += " and Stocks " + q.Cond + " " + q.Quantity;

            string orderby = "GoodsID desc ";
            if (q.Sorts == 1)
                orderby = "Stocks desc,GoodsID desc";
            else if (q.Sorts == 2)
                orderby = "Stocks ,GoodsID desc";
            return dalStocks.QueryListAsPageEx(q.StoreID, cond, orderby, q.CurPage, q.PageSize, out q.TotalCount);
        }
        #endregion

        #region "查询指定商品，指定库房的进出记录； by  赵国伟 2012-06-10"
        /// <summary>
        /// 库存进出记录；
        /// </summary>
        /// <param name="goodsId"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public List<GoodsStockRcordInfo> QueryStockRecords(int goodsId, int storeId)
        {
            string cond = "GoodsID=" + goodsId;
            if (storeId > 0)
                cond += " and StoreID=" + storeId;

            List<GoodsStockRcords> source = dalRecords.QueryList(cond, "RecordDate desc");

            List<GoodsStockRcordInfo> target = null;
            if (source != null)
            {
                target = new List<GoodsStockRcordInfo>();
                string goodsName = DALHelper.GetFieldValue<Goods>(goodsId, GoodsEnum.GoodsName.ToString());
                Dictionary<int, string> dictStore = QueryStoresToHash();
                foreach (GoodsStockRcords s in source)
                {
                    GoodsStockRcordInfo t = new GoodsStockRcordInfo();
                    t.RecordInfo = s;
                    t.GoodsName = goodsName;
                    t.StoreName = dictStore[s.StoreID];
                    target.Add(t);
                }

            }
            return target;
        }
        #endregion

        #region "分页查询入库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 分页查询入库单信息；
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public List<StockInsInfo> QueryStockInsAsPage(QueryStockInsAsPage q)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(q.StockInSN))
                cond += " and StockInID like '%" + q.StockInSN + "%'";
            if (q.StoreID > 0)
                cond += " and StoreID=" + q.StoreID;
            if (q.SupplierID > 0)
                cond += " and SupplierID=" + q.SupplierID;
            if (!string.IsNullOrEmpty(q.StartDate))
                cond += " and EnterDate>='" + q.StartDate + "'";
            if (!string.IsNullOrEmpty(q.EndDate))
                cond += " and EnterDate <='" + q.EndDate + "'";

            if (!string.IsNullOrEmpty(q.Status))
                cond += " and Status='" + q.Status + "'";

            List<StockIns> source = dalIns.QueryListAsPage(cond, "StockInID desc", q.CurPage, q.PageSize, out q.TotalCount);
            List<StockInsInfo> target = null;
            if (source != null)
            {
                target = new List<StockInsInfo>();
                Dictionary<int, string> dictStore = QueryStoresToHash();
                Dictionary<int, string> dictSupplier = BLLHelper.GenerateToHash(DALHelper.QueryIDNameObjs<Suppliers>("SupplierID in (Select SupplierID From StockIns Where " + cond + ")", SuppliersEnum.SupplierName));
                foreach (StockIns d in source)
                {
                    StockInsInfo targetInfo = new StockInsInfo();
                    targetInfo.InsInfo = d;
                    targetInfo.StoreName = dictStore[d.StoreID];
                    targetInfo.SupplierName = dictSupplier[d.SupplierID];
                    target.Add(targetInfo);
                }
            }
            return target;
        }
        #endregion

        #region "分页查询出库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 分页查询出库单信息；
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public List<StockOutsInfo> QueryStockOutsAsPage(QueryStockOutsAsPage q)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(q.StockOutSN))
                cond += " and StockOutID like '%" + q.StockOutSN + "%'";
            if (q.StoreID > 0)
                cond += " and StoreID=" + q.StoreID;
            if (!string.IsNullOrEmpty(q.StartDate))
                cond += " and EnterDate>='" + q.StartDate + "'";
            if (!string.IsNullOrEmpty(q.EndDate))
                cond += " and EnterDate <='" + q.EndDate + "'";

            if (!string.IsNullOrEmpty(q.Status))
                cond += " and Status='" + q.Status + "'";

            List<StockOuts> source = dalOuts.QueryListAsPage(cond, "StockOutID desc", q.CurPage, q.PageSize, out q.TotalCount);
            List<StockOutsInfo> target = null;
            if (source != null)
            {
                target = new List<StockOutsInfo>();
                Dictionary<int, string> dictStore = QueryStoresToHash();
                foreach (StockOuts d in source)
                {
                    StockOutsInfo targetInfo = new StockOutsInfo();
                    targetInfo.OutsInfo = d;
                    targetInfo.StoreName = dictStore[d.StoreID];
                    target.Add(targetInfo);
                }
            }
            return target;
        }
        #endregion




        #region "查询出指定的入库单信息；by 赵国伟 2012-06-10"
        /// <summary>
        /// 查询出指定的入库单信息；
        /// </summary>
        /// <param name="stockInsId"></param>
        /// <returns></returns>
        public StockInsInfo GetStockInInfo(int stockInsId)
        {
            StockIns s = dalIns.GetModel(stockInsId);
            StockInsInfo t= null;
            if (s != null)
            {
                t = new StockInsInfo();
                t.InsInfo = s;
                t.StoreName = dalStores.GetName(s.StoreID);
                t.SupplierName = DALHelper.GetFieldValue<Suppliers>(s.SupplierID, SuppliersEnum.SupplierName.ToString());
                t.Lines = dalLines.QueryListEx("StockLineID in(Select StockLineID From R_StockIns_Lines Where StockInID=" + s.StockInID + ")");
                
            }
            return t;
        }
        #endregion

        #region "查询一条出库单信息；  by 赵国伟 2012-06-10"
        /// <summary>
        /// 查询一条出库单信息；
        /// </summary>
        /// <param name="stockOutId"></param>
        /// <returns></returns>
        public StockOutsInfo GetStockOutInfo(int stockOutId)
        {
            StockOuts s = dalOuts.GetModel(stockOutId);
            StockOutsInfo t = null;
            if (s != null)
            {
                t = new StockOutsInfo();
                t.OutsInfo = s;
                t.StoreName = dalStores.GetName(s.StoreID);
                t.Lines = dalLines.QueryListEx("StockLineID in(Select StockLineID From R_StockOuts_Lines Where StockOutID=" + s.StockOutID + ")");
            }
            return t;
        }
        #endregion

        #region "保存入库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 保存入库单信息；
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ReturnValue SaveStockInInfo(StockInsInfo data)
        {
            ReturnValue ret = new ReturnValue();
            if (data.InsInfo.StockInID > 0)
            {
                StockIns o =  dalIns.GetModel(data.InsInfo.StockInID);

                if (o.Status == StockInOutStatus.已执行.ToString() )
                {
                    ret.Message = "该入库单已执行，不能再修改！";
                    return ret;
                }

              
                
                data.InsInfo.CreateDate = o.CreateDate;
            }
            return dalIns.Save(data);
        }
        #endregion

        #region "保存出库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 保存出库单信息；
        /// </summary>
        /// <param name="outInfo"></param>
        /// <returns></returns>
        public ReturnValue SaveStockOutInfo(StockOutsInfo data)
        {
            ReturnValue ret = new ReturnValue();
            if (data.OutsInfo.StockOutID > 0)
            {
                StockOuts o = dalOuts.GetModel(data.OutsInfo.StockOutID);

                if (o.Status == StockInOutStatus.已执行.ToString())
                {
                    ret.Message = "该出库单已执行，不能再修改！";
                    return ret;
                }

                
                
                data.OutsInfo.CreateDate = o.CreateDate;
            }
            return dalOuts.Save(data);
           
        }
        #endregion

        #region "删除一条指定的入库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 删除一条指定的入库单信息；
        /// </summary>
        /// <param name="stockInId"></param>
        /// <returns></returns>
        public ReturnValue DeleteStockIn(int stockInId)
        {
            return dalIns.DeleteStockIn(stockInId);
        }
        #endregion

        #region "删除一条指定的出库单信息； by 赵国伟 2012-06-10"
        /// <summary>
        /// 删除一条指定的出库单信息；
        /// </summary>
        /// <param name="stockOutId"></param>
        /// <returns></returns>
        public ReturnValue DeleteStockOut(int stockOutId)
        {
            return dalOuts.DeleteStockOut(stockOutId);
        }
        #endregion


    }
}
